home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Converters / Convert_MacPaint / Source / PSstuff / PackedCode < prev    next >
Text File  |  1995-06-12  |  3KB  |  115 lines

  1. %BEGIN MacPaint
  2.  
  3. %
  4. %    Define values for: the 'length' byte and a byte when a run is found 
  5. %    define a string to be used when reading a byte that has been repeated 
  6.  
  7. /lengthByte 1 string def
  8. /runByte 1 string def
  9.  
  10. %
  11. %    - unpackbytes -
  12. %
  13. %    This unpacks packed hex data into binary strings. Data format is
  14. %    identical to PS level 2 RLE and the same as Mac PackBits (plus
  15. %    terminating 80). Data is divided into chunks.  Each chunk starts with
  16. %    a 'length' byte. if byte is < 128 use that number of following bytes
  17. %    (plus one) literally.  if byte is > 128, next byte should be used
  18. %    257-(thatbyte) times 128 (80) is improperly dealt with here if it
  19. %    appears in the data (it is an error when it does) Ignoring 128 saves
  20. %    time, tho, and all files using this are well formed and don't use it.
  21. /unpackbytes 
  22.     % get the length byte 
  23.     /thelength 
  24.         currentfile lengthByte readhexstring pop 
  25.         0 get 
  26.     def 
  27.  
  28.     thelength 128 lt 
  29.     { 
  30.         % the length byte says copy thelength bytes literally
  31.         /dataString thelength 1 add  string def 
  32.         currentfile dataString readhexstring pop 
  33.     }
  34.     { 
  35.         % the length byte says copy next byte 257-thelength times 
  36.         /runlength 257 thelength sub def 
  37.         /runString runlength  string def
  38.         currentfile runByte readhexstring pop pop 
  39.  
  40.         0 1 runlength 1 sub 
  41.         { runString exch runByte putinterval } 
  42.         for 
  43.         runString 
  44.     } 
  45.     ifelse 
  46. } def 
  47.  
  48.  
  49. %
  50. %    - level1showimage -
  51. %
  52. %    This reads the packed data in, unpacks it, and displays the image
  53. %    properly. It also reads and discards the terminating byte (80).
  54. %    and > character which is eof for the asciihexdecode filter.
  55. /level1showimage 
  56.     576 720 scale 
  57.     576 720 1 
  58.     [576 0 0 -720 0 720] 
  59.     {unpackbytes} 
  60.     image
  61.     % Discard the final 80 byte.
  62.     currentfile lengthByte readhexstring pop pop
  63.     currentfile read pop pop
  64. } def
  65.  
  66.  
  67.  
  68.  
  69. %
  70. %    - level2showimage -
  71. %
  72. %    This reads the image data in, and displays the image properly,
  73. %    letting a runlength filter decode the packed data.  Since this is
  74. %    PS level 2, we don't need the data source to be a procedure.
  75. /level2showimage 
  76.     576 720 scale 
  77.     576 720 1 
  78.     [576 0 0 -720 0 720] 
  79.     currentfile /ASCIIHexDecode filter /RunLengthDecode filter
  80.     image
  81. } def
  82.  
  83.  
  84. %
  85. %    PSlevel
  86. %
  87. %    This stores a number describing the PS level of the current
  88. %    environment. If possible, it uses the level2 langlevel,
  89. %    otherwise it assumes 1
  90. %
  91. /PSlevel
  92.     systemdict /languagelevel known
  93.         {languagelevel}
  94.         {1}
  95.     ifelse
  96. def
  97.  
  98. /showMPimage
  99. {
  100.     PSlevel 2 lt
  101.         { level1showimage }
  102.         { level2showimage }
  103.     ifelse
  104. }
  105. def
  106.  
  107. %END MacPaint
  108.  
  109.